package xx.shiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import xx.shiro.entity.SysPermissions;
import xx.shiro.entity.SysRole;
import xx.shiro.entity.SysUser;
import xx.shiro.mapper.SysPermissionsMapper;
import xx.shiro.mapper.SysRoleMapper;
import xx.shiro.mapper.SysUserMapper;
import xx.shiro.service.LoginService;

import javax.annotation.Resource;
import java.security.Permissions;
import java.util.*;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    SysPermissionsMapper sysPermissionsMapper;

    @Override
    public SysUser queryUserInfoByUserName(String userName) {
        SysUser sysUser =  this.sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username",userName));
        List<SysRole> roleList = sysRoleMapper.querySysRoleByUserId(sysUser.getId());
        Set<SysPermissions> sysPermissionsSet = new HashSet<>();
        roleList.forEach(role->{
            sysPermissionsSet.addAll(this.sysPermissionsMapper.queryPermissionByRoleId(role.getId()));
        });
        sysUser.setSysRoleList(roleList);
        sysUser.setPermissionsSet(sysPermissionsSet);
        return sysUser;
    }
}
